<?php

namespace App;

use App\model\customer\CustomerBigDepart;
use App\model\customer\CustomerSecondDepart;
use App\model\UserDepartment;
use App\model\UserRole;
use App\model\Workshop;

use Encore\Admin\Traits\ModelTree;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;
use Tymon\JWTAuth\Contracts\JWTSubject;

/**
 * App\User
 *
 * @property int $id
 * @property string $name
 * @property string $email
 * @property \Illuminate\Support\Carbon|null $email_verified_at
 * @property string $password
 * @property string|null $remember_token
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
 * @property-read int|null $notifications_count
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereEmailVerifiedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereRememberToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property int|null $role_id 角色id
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereRoleId($value)
 * @property-read \App\model\UserRole|null $role
 * @property string|null $userid 成员UserID。对应管理端的帐号
 * @property string|null $openid
 * @property int|null $status 激活状态: 1=已激活，2=已禁用，4=未激活 已激活代表已激活企业微信或已关注微工作台（原企业号）。未激活代表既未激活企业微信又未关注微工作台（原企业号）。
 * @property string|null $qr_code 员工个人二维码，扫描可添加为外部联系人
 * @property int|null $type 0 企业微信  1 小程序
 * @property int|null $gender 性别。0表示未定义，1表示男性，2表示女性
 * @property string|null $mobile 手机号
 * @property string|null $avatar 头像
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\model\UserDepartment[] $department
 * @property-read int|null $department_count
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereAvatar($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereGender($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereMobile($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereOpenid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereQrCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereUserid($value)
 * @property string|null $position 职位
 * @property int|null $is_leader_in_dept
 * @property string|null $session_key
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereIsLeaderInDept($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User wherePosition($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereSessionKey($value)
 * @property int|null $workshop_num 发布的房源数量
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereWorkshopNum($value)
 * @property int|null $tiyan
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereTiyan($value)
 * @property int|null $parent_id
 * @property string|null $sort
 * @property int|null $auth_map 1
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\User[] $children
 * @property-read int|null $children_count
 * @property-read \App\User|null $parent
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereAuthMap($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereParentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereSort($value)
 * @property-read int|null $permissions_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Permission\Models\Role[] $roles
 * @property-read int|null $roles_count
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User permission($permissions)
 * @property int|null $department_id
 * @property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Permission\Models\Permission[] $permissions
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereDepartmentId($value)
 * @property int|null $number
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereNumber($value)
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\model\customer\CustomerBigDepart[] $big_groups
 * @property-read int|null $big_groups_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\model\customer\CustomerSecondDepart[] $groups
 * @property-read int|null $groups_count
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User role($roles, $guard = null)
 */
class User extends Authenticatable implements JWTSubject
{
    //use Notifiable,ModelTree,HasRoles;
    use Notifiable,ModelTree, HasRoles;
    const VIEW_ALL=0;
    const VIEW_ONE=1;
    const VIEW_CHILDREN=2;
    public static $authMap=[
      self::VIEW_ALL=>'查看所有人的数据',
      self::VIEW_ONE=>'只查看自己的数据',
      self::VIEW_CHILDREN=>'查看组数据',
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        $this->setParentColumn('parent_id');
        $this->setOrderColumn('sort');
        $this->setTitleColumn('name');
    }

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }
    public function getJWTCustomClaims()
    {
        return [];
     }

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * 角色
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function role(){
        return $this->belongsTo(UserRole::class,'role_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function department(){
        return $this->belongsToMany(UserDepartment::class,'user_department_id','user_id','department_id',null,'department_id');
    }

    /**
     * 查看数据权限
     */
    public function  groups(){
        return   $this->belongsToMany(CustomerSecondDepart::class,'user_group','user_id','group_id');
    }

    /**
     * 查看大组数据权限
     */
    public function  big_groups(){
        return   $this->belongsToMany(CustomerBigDepart::class,'user_big_group','user_id','group_id');
    }
}
